#include "path.h"

Path::Path(){
	this->id=0;
}

Path::~Path(){
}

unsigned int Path::createPathIterator(unsigned int step,bool loop){
	this->paths.insert(std::make_pair(id,PathIterator(*this->begin(),step,loop)));
	return id++;
}

void Path::deletePathIterator(unsigned int id){
	this->paths.erase(id);
}

void Path::stepPathIterator(unsigned int id){
	if(this->paths.find(id)!=this->paths.end()){
		Path::iterator it = std::find(this->begin(),this->end(),this->paths[id].initial);
		++it;

		if(it==this->end() && !this->paths[id].loop){
			return;
		}else if(it==this->end()){
			it=this->begin();
		}

		Path::iterator it2 = ++it;
		PathIterator& pid = this->paths[id];

		++pid.count;
		if(pid.count>=pid.step){
			if(pid.newStep!=0){
				pid.step = pid.newStep;
				pid.newStep = 0;
			}

			pid.initial = *it;

			it2=++it;
			if(it2==this->end()){
				it2=++this->begin();
			}

			pid.count=0;
		}
		pid.last = *it2;

		pid.current = ((((*it)-pid.initial)*(1/(float)pid.step))*(float)pid.count)+pid.initial;
		pid.updateFollowers();
	}
}

Point Path::getCurrent(unsigned int id){
	if(this->paths.find(id)!=this->paths.end()){
		return this->paths[id].current;
	}else
		throw std::string("No se creo ese iterador");
}

void Path::addFollowerToPathIterator(unsigned int id,IFollower* follower){
		if(this->paths.find(id)!=this->paths.end())
			this->paths[id].followers.push_back(follower);
}

void Path::removeFollowerFromPathIterator(unsigned int id,IFollower* follower){
	if(this->paths.find(id)!=this->paths.end())
		this->paths[id].followers.remove(follower);
}

void Path::modifyStep(unsigned int id,unsigned int delta_step){
	if(this->paths.find(id)==this->paths.end())
		return;

	this->paths[id].setNewStep(delta_step);
}
